package com.dong.service;

import java.sql.Connection;
import java.util.List;

import com.dong.dao.UserDAO;
import com.dong.dao.impl.UserDAOImpl;
import com.dong.util.db.Database;
import com.dong.util.mail.MailSenderInfo;
import com.dong.util.mail.SimpleMailSender;
import com.dong.vo.User;
import com.sun.jndi.url.corbaname.corbanameURLContextFactory;

public class UserService {
	private UserDAO userDAO = new UserDAOImpl();

	// 添加新用户
	public void addUser(User user, String basePath) throws Exception {
		Connection connection = null;
		StringBuffer body = new StringBuffer();
		StringBuffer link = new StringBuffer();
		body.append("您在东东网站新注册的用户名是：");
		body.append(user.getUid());
		body.append(",请点击以下链接激活账号!\n");
		link.append(basePath);
		link.append("ActiveUserServlte?uid=");
		link.append(user.getUid());
		link.append("&validateCode=");
		link.append(user.getValidateCode());
		body.append(link.toString());
		try {
			connection = Database.getConnection();
			userDAO.setConnection(connection);
			userDAO.addUser(user);
			// 发送激活链接的邮件
			 MailSenderInfo mailInfo = new MailSenderInfo();    
		     mailInfo.setMailServerHost("smtp.qq.com");    
		     mailInfo.setMailServerPort("25");    
		     mailInfo.setValidate(true);    
		     mailInfo.setUserName("348106963@qq.com");    
		     mailInfo.setPassword("dongqing1314love");// �����������
		     mailInfo.setFromAddress("348106963@qq.com");    
		     mailInfo.setToAddress("348106963@qq.com");    
		     mailInfo.setSubject("XX网站注册激活");    
		     mailInfo.setContent(body.toString());    
		     SimpleMailSender sms = new SimpleMailSender();   
			 sms.sendTextMail(mailInfo);//���������ʽ
			// System.out.println("TEXT���ͳɹ���");
//		     sms.sendHtmlMail(mailInfo);// ����html��ʽ
//		     System.out.println("HTML���ͳɹ���");
			
			Database.commit();
		} catch (Exception e) {
			e.printStackTrace();
			Database.rollback();
			throw e;
		} finally {
			Database.releaseConnection(connection);
		}
	}

	// 删除指定用户
	public void deleteUser(String uid) throws Exception {
		Connection connection = null;
		try {
			connection = Database.getConnection();
			userDAO.setConnection(connection);
			userDAO.deleteUser(uid);
			Database.commit();
		} catch (Exception e) {
			e.printStackTrace();
			Database.rollback();
			throw e;
		} finally {
			Database.releaseConnection(connection);
		}
	}

	// 更新用户信息
	public void updateUser(User user) throws Exception {
		Connection connection = null;
		try {
			connection = Database.getConnection();
			userDAO.setConnection(connection);
			userDAO.updateUser(user);
			Database.commit();
		} catch (Exception e) {
			e.printStackTrace();
			Database.rollback();
			throw e;
		} finally {
			Database.releaseConnection(connection);
		}
	}

	// 取得所有用户列表
	public List<User> listAllUser() throws Exception {
		Connection connection = null;
		List<User> list = null;
		try {
			connection = Database.getConnection();
			userDAO.setConnection(connection);
			list = userDAO.lisetAllUser();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			Database.releaseConnection(connection);
		}
		return list;
	}

	// 根据用户名返回用户对象
	public User getUser(String uid) throws Exception {
		Connection connection = null;
		User user = null;
		try {
			connection = Database.getConnection();
			userDAO.setConnection(connection);
			user = userDAO.getUser(uid);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			Database.releaseConnection(connection);
		}
		return user;
	}

	// 检查登录名及密码
	public boolean login(String uid, String password) throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = Database.getConnection();
			System.out.println("测试点1");
			userDAO.setConnection(connection);
			System.out.println("测试点2");
			User user = userDAO.getUser(uid);			
			System.out.println("测试点3");
			if (user != null) {
				System.out.println("测试点41");
				if (password.equals(user.getPassword()) && user.getValidateCode().equals("activated")) {
					// 用户存在，且账号密码正确，登录成功，这时在登录次数字段加1,然后更新;
					System.out.println("测试点4");
					int loginNum = user.getLoginNum() + 1;					
					user.setLoginNum(loginNum);
					System.out.println("测试点5");
					userDAO.updateUser(user);
					System.out.println("测试点6");
					Database.commit();
					System.out.println("测试点7");
					flag = true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			Database.releaseConnection(connection);
		}
		return flag;
	}

	// 检查用户名是否已存在
	public boolean checkUid(String uid) throws Exception {
		Connection connection = null;
		// 默认用户存在,返回true
		boolean flag = true;
		try {
			connection = Database.getConnection();
			userDAO.setConnection(connection);
			User user = userDAO.getUser(uid);
			// 如果查询出为null，表明用户不存在，即可以注册
			if (user == null) {
				flag = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			Database.releaseConnection(connection);
		}
		return flag;
	}

	// 激活用户(即验证发往邮箱的验证码)
	public boolean validateUser(String uid, String validateCode)
			throws Exception {
		Connection connection = null;
		// 默认为激活失败
		boolean flag = false;
		try {
			connection = Database.getConnection();
			userDAO.setConnection(connection);
			User user = userDAO.getUser(uid);
			if (user != null) {
				if (user.getValidateCode().equals(validateCode)) {
					user.setValidateCode("activated");
					userDAO.updateUser(user);
					Database.commit();
					flag = true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Database.rollback();
			throw e;
		}
		return flag;
	}

	// UserDAO get/set method
	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
}
